<?php
namespace Live\Controller;
use Think\Controller;

class CommonController extends Controller {
    public function __construct(){
        parent::__construct();
        if(!session('liveadmin_uid')){
            $this->error('请登录后访问',U('Public/login'));
            //exit("<script>alert('请登录后访问');top.location.href='".U('Public/login')."'</script>");
        }

        // $this->_authPower();
    }

    /**
     * 验证权限
     * @return [type] [description]
     */
    protected function _authPower() {
    	$uid = session('liveadmin_uid');
    	$userInfo = M('Admin')->field('roleid, adminauth')->where(array('id' => $uid))->find();
    	$userAuthIds = $userInfo['adminauth']; //用户自身的权限
    	$userRoleIds = M('AdminRole')->where(array('id' => $userInfo['roleid']))->getField('auth'); // 角色权限

    	$ids = array_merge(explode(',', $userAuthIds), explode(',', $userRoleIds));
    	$ids = array_unique($ids);

    	$iMap['id'] = array('in', $ids);
    	$rights = M('AdminAuth')->where($iMap)->select();

    	$control = strtolower(CONTROLLER_NAME) ;
    	$action = strtolower(ACTION_NAME);
    	$flag = $control . '_' . $action;
    	$authList = array();

    	foreach($rights as $val) {	
    		$authList[] = $val['value'];
    	}

    	if(!in_array($flag, $authList)) {
    		/*echo '<script>alert("无权限");</script>';
    		U('Public/home');*/
			$this->error('请登录后访问',U('Public/login'));
			//exit("<script>alert('请登录后访问');top.location.href='".U('Public/login')."'</script>");
    	}
    }


} 